<template>
  <div class="content">
    <div class="header_box">
      <div class="header_left">字典管理</div>
      <div class="header_right">
        <el-form :inline="true" :model="searFrom" class="form_box">
          <el-form-item>
            <el-input
              v-model="searFrom.name"
              placeholder="字典名称"
              style="width: 200px"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="searFrom.code"
              placeholder="字典类型"
              style="width: 200px"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="init">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="addNew">新增</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{ background: '#fafbfc', color: '#bbbcc3' }"
      max-height="500"
      @selection-change="handleSelectionChange"
    >
      <el-table-column prop="id" label="字典编号" width="100px" />
      <el-table-column prop="name" label="字典名称" />
      <el-table-column prop="type" label="字典类型">
        <template #default="{ row }">
          <span class="clickable" @click="navigateToDictionaryData(row.id)">
            {{ row.type }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <div class="icon_content">
            <el-tooltip
              class="box-item"
              effect="dark"
              content="字典的"
              placement="top-start"
            >
              <div class="icon_box">
                <el-icon @click="edit(row)" size="20" color="#71778d"
                  ><EditPen
                /></el-icon>
              </div>
            </el-tooltip>
            <el-tooltip
              class="box-item"
              effect="dark"
              content="删除"
              placement="top-start"
            >
              <div class="icon_box_d">
                <el-icon @click="remove(row)" size="20" color="#71778d"
                  ><Remove
                /></el-icon>
              </div>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="page_box">
      <el-pagination
        v-model:current-page="searFrom.pageNo"
        v-model:page-size="searFrom.pageSize"
        :page-sizes="[10, 50, 100, 150]"
        :small="small"
        :disabled="false"
        :background="true"
        layout="sizes, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
  <el-dialog :title="dialogTitle" v-model="repairVis" width="500">
    <el-form label-position="right" :model="objectForm">
      <el-form-item label="字典名称">
        <el-input v-model="objectForm.name" />
      </el-form-item>
      <el-form-item label="字典类型">
        <el-input v-model="objectForm.type" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="objectForm.remark" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="repairVis = false">取消</el-button>
        <el-button type="primary" @click="confirmRepair"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineOptions, ref, onBeforeMount } from "vue";
import * as dictionaryApi from "../../../api/project3/dictionary/dictionary.js";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import confirm from "../../../utils/ElMessageUtils.js";

defineOptions({ name: "dictionary" });

const tableData = ref([
  {
    id: 1,
    name: "用户性别",
    type: "sys_user_sex",
    status: "正常",
    remark: "用户性别列表",
  },
  {
    id: 2,
    name: "菜单状态",
    type: "sys_show_hide",
    status: "正常",
    remark: "菜单状态列表",
  },{
    id: 3,
    name: "级别",
    type: "sys_level",
    status: "正常",
    remark: "级别",
  },
  // 可以根据需要添加更多初始化数据
]);

const objectForm = ref({
  name: "",
  type: "",
  status: "正常",
  remark: "",
});

const router = useRouter();

let searFrom = ref({
  pageNo: 1,
  pageSize: 10,
  excludeCode: "admin",
});

let total = ref(0);

let repairVis = ref(false);

const selectedRows = ref([]);

const dialogTitle = ref("编辑字典");

const init = async () => {
  const res = await dictionaryApi.page(searFrom.value);
  const { records } = res;
  tableData.value = records;
  total.value = res.total;
};

const confirmRepair = async () => {
  const res = await dictionaryApi.saveOrUpdate(objectForm.value);
  const { code, msg } = res;
  if (code == "200") {
    ElMessage.success("维修操作成功");
    repairVis.value = false;
    init();
    return;
  }
  ElMessage.error(msg);
};

const remove = async (row) => {
  const b = await confirm("您确定要删除吗？");
  if (!b) {
    return;
  }
  const res = await dictionaryApi.remove({ id: row.id });
  const { code, msg } = res;
  if (code == "200") {
    ElMessage.success("操作成功");
    init();
    return;
  }
  ElMessage.error(msg);
};

const edit = (row) => {
  console.log("编辑行数据:", row);
  objectForm.value = { ...row };
  dialogTitle.value = "编辑字典";
  repairVis.value = true;
};

const handleSelectionChange = (val) => {
  selectedRows.value = val;
};

const navigateToDictionaryData = (id) => {
  router.push({ name: "DictionaryData", params: { id } });
};

const addNew = () => {
  objectForm.value = {
    name: "",
    type: "",
    status: "正常",
    remark: "",
  };
  dialogTitle.value = "新增字典";
  repairVis.value = true;
};

onBeforeMount(() => {
  // init();
});
</script>

<style lang="less" scoped>
.header_right {
  background-color: #ffffff;
  justify-content: flex-end;
  border-radius: 0px 15px 0px 0px;
}
:deep(.form_box) {
  width: 80%;
  height: 40%;
  margin: 0px;
  display: flex;
  justify-content: flex-end;
}
.header_left {
  border-radius: 15px 0px 0px 0px;
  font-weight: 700;
}
.clickable {
  color: #409eff;
  cursor: pointer;
}
</style>